package com.vf.admin.platform.controller;

import com.vf.admin.common.BaseController;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.platform.entity.BaoMing;
import com.vf.admin.platform.service.BaoMingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/demo")
public class DemoController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private BaoMingService baoMingService;
    @Resource
    RedisUtil redisUtil;

    /**
     * 进入编辑页面
     */
    @RequestMapping("/editPage")
    public String editPage(Integer id,Model model){
        try{
            if(id!=null && id>0){
                BaoMing baoMing = baoMingService.get(id);
//                request.setAttribute("baoMing",baoMing);
                model.addAttribute("baoMing",baoMing);
            }
            return "/platform/demo/editpage";
        }catch(Exception e){
            this.setMessage(ERROR_MES);
            this.setUrl("/platform/demo/editpage");
            return ERROR; // 跳转时返回return字符串
        }
    }

    /**
     * 保存或者更新
     */
    @RequestMapping(value="/save")
    public String save(BaoMing baoMing){
        try{
            if(baoMing!=null && baoMing.getId()!=null){
                BaoMing baoMingBf = baoMingService.get(baoMing.getId());
                baoMingBf.setXingming(baoMing.getXingming());
                baoMingBf.setUserid(baoMing.getUserid());
                baoMingBf.setShijian(new Date());
                baoMingService.saveOrUpdate(baoMingBf);
            }else{
                baoMing.setShijian(new Date());
                baoMingService.saveOrUpdate(baoMing);
            }
            this.setRefresh(false); // 设置父页面是否刷新,默认为false(用户弹出层编辑页面更新操作时，刷新父窗口)
            this.setUrl("/demo/list");
            return SUCCESS;
        }catch(Exception e){
            this.setUrl("/demo/list");
            return ERROR; // 跳转时返回return字符串
        }
    }

    /**
     * 列表查询
     */
    @RequestMapping("/list")
    @ResponseBody
    public List list(){
        try{
            Map<String,Object> paraMap = new HashMap<String,Object>();
//            paraMap.put("xingming","张三");//模糊查询
            List<BaoMing> baoMingList = baoMingService.findList(paraMap);
           return baoMingList;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
